﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Linq;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using ALiteTeam.PackageTracer.Core;
using ALiteTeam.PackageTracer.Core.Impl;
using GalaSoft.MvvmLight.Messaging;
using ALiteTeam.PackageTracer.Data;
using GalaSoft.MvvmLight.Command;
using Microsoft.Phone.Controls;

namespace ALiteTeam.PackageTracer.ViewModel
{
    public class InputOrderViewModel : ViewModelBase
    {
        private Delivery _Delivery;

        public readonly static Guid INITIALIZE = Guid.NewGuid();

        public IDeliveryOperate DeliveryOp { get; set; }
        public IOrderOperate OrderOp { get; set; }
        public IQueryService QueryOp { get; set; }

        public InputOrderViewModel()
        {
            if (IsInDesignMode == false)
            {
                this.DeliveryOp = DeliveryDataOp.Instance;
                this.OrderOp = new OrderOp();

                Messenger.Default.Register<Guid>(this, INITIALIZE, Initialize);

                this.GoBackCommand = new RelayCommand(GoBack);
                this.GoNextCommand = new RelayCommand(GoNext);
                this.GoScanerCommand = new RelayCommand(GoScaner);
            }
        }

        #region 绑定属性

        /// <summary>
        /// The <see cref="OrderNo" /> property's name.
        /// </summary>
        public const string OrderNoPropertyName = "OrderNo";
        private string mOrderNo = string.Empty;

        /// <summary>
        /// Gets the OrderNo property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string OrderNo
        {
            get
            {
                return mOrderNo;
            }
            set
            {
                if (mOrderNo == value)
                {
                    return;
                }
                mOrderNo = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(OrderNoPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="DisplayName" /> property's name.
        /// </summary>
        public const string DisplayNamePropertyName = "DisplayName";
        private string mDisplayName = string.Empty;

        /// <summary>
        /// Gets the DisplayName property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string DisplayName
        {
            get
            {
                return mDisplayName;
            }
            set
            {
                if (mDisplayName == value)
                {
                    return;
                }
                mDisplayName = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(DisplayNamePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="LogoFile" /> property's name.
        /// </summary>
        public const string LogoFilePropertyName = "LogoFile";
        private string mLogoFile = string.Empty;

        /// <summary>
        /// Gets the LogoFile property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string LogoFile
        {
            get
            {
                return mLogoFile;
            }
            set
            {
                if (mLogoFile == value)
                {
                    return;
                }
                mLogoFile = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(LogoFilePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="PhoneNumber" /> property's name.
        /// </summary>
        public const string PhoneNumberPropertyName = "PhoneNumber";
        private string mPhoneNumber = string.Empty;

        /// <summary>
        /// Gets the PhoneNumber property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string PhoneNumber
        {
            get
            {
                return mPhoneNumber;
            }
            set
            {
                if (mPhoneNumber == value)
                {
                    return;
                }
                mPhoneNumber = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(PhoneNumberPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="WebSite" /> property's name.
        /// </summary>
        public const string WebSitePropertyName = "WebSite";
        private string mWebSite = string.Empty;

        /// <summary>
        /// Gets the WebSite property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string WebSite
        {
            get
            {
                return mWebSite;
            }
            set
            {
                if (mWebSite == value)
                {
                    return;
                }
                mWebSite = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(WebSitePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="IntroPropertyName" /> property's name.
        /// </summary>
        public const string IntroPropertyName = "Intro";
        private string mIntro = string.Empty;

        /// <summary>
        /// Gets the MyPropeIntrorty property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string Intro
        {
            get
            {
                return mIntro;
            }

            set
            {
                if (mIntro == value)
                {
                    return;
                }
                mIntro = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(IntroPropertyName);
            }
        }

        #endregion

        /// <summary>
        /// 根据DeliveryID消息加载快递商信息
        /// </summary>
        /// <param name="deliveryID"></param>
        private void Initialize(Guid deliveryID)
        {
            Settings.AppContext.SelectedDeliveryID = deliveryID;

            _Delivery = this.DeliveryOp.DeliveryResource.Deliveries.Where(e => e.ID == deliveryID).FirstOrDefault();

            if (Settings.AppContext.CurrentDeliveryOrder == null)
            {
                Settings.AppContext.CurrentDeliveryOrder = new DeliveryOrder()
                {
                    DeliveryOrderID = Guid.NewGuid(),
                    DeliveryID = _Delivery.ID,
                    IsAutoRefresh = false,
                    RefreshDuration = 2
                };
            }
            else
            {
                this.OrderNo = Settings.AppContext.CurrentDeliveryOrder.OrderNo;
            }

            this.DisplayName = _Delivery.DisplayName;
            this.LogoFile = _Delivery.LogoFile;
            this.PhoneNumber = _Delivery.Phone;
            this.WebSite = _Delivery.WebSite;
            this.Intro = _Delivery.Intro;
        }

        public RelayCommand GoBackCommand { get; private set; }
        private void GoBack()
        {
            if (string.IsNullOrEmpty(this.OrderNo) == false)
            {
                var result = MessageBox.Show("该操作会导致放弃本次添加，是否继续？", "警告", MessageBoxButton.OKCancel);
                if (result == MessageBoxResult.OK)
                {
                    // 清空本次运单登记信息
                    Settings.AppContext.CurrentDeliveryOrder = null;
                    (Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).GoBack();
                }
            }
            else
            {
                // 清空本次运单登记信息
                Settings.AppContext.CurrentDeliveryOrder = null;
                (Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).GoBack();
                //(Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Navigate(new Uri("/View/MainPage.xaml?Index=1", UriKind.Relative));
            }
        }

        public RelayCommand GoNextCommand { get; private set; }
        private void GoNext()
        {
            // 存放OrderNo到Setting
            Settings.AppContext.CurrentDeliveryOrder.OrderNo = this.OrderNo;

            // 给ComfirmOrderViewModel发送初始化消息
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this.OrderNo), ComfirmOrderViewModel.INITIALIZE);

            // 转向ComfirmOrder页面
            (Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Navigate(new Uri("/View/ComfirmOrder.xaml", UriKind.Relative));
        }

        public RelayCommand GoScanerCommand { get; private set; }
        private void GoScaner()
        {
            (Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Navigate(new Uri("/View/Scaner.xaml", UriKind.Relative));
        }
    }
}
